package weight;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.common.R;

/**
 * Created by ltx on 17/6/30
 * 自定义弹框
 */

public class ToastManger {
    private TextView mTvToastMessage;
    private ImageView mIvToastType;
    private static ToastManger mInstance;
    private static Context mContext;

    private View mToastRoot;

    private Toast mToast;

    public final static int S_TOAST_NONE=0;//没有
    public final static int S_TOAST_SUCCESS=1;
    public final static int S_TOAST_FAIL=2;

    public static ToastManger getInstance(Context context){
        if(null==mInstance){
            mContext=context.getApplicationContext();
            mInstance=new ToastManger();
        }
        return mInstance;
    }
    private ToastManger(){
        mToastRoot = View.inflate(mContext,R.layout.ll_custom_toast,null);
        //初始化布局控件
        mTvToastMessage = (TextView) mToastRoot.findViewById(R.id.tv_custom_toast);
        mIvToastType = (ImageView) mToastRoot.findViewById(R.id.iv_custom_toast);
        mToast = new Toast(mContext);
        //获取屏幕高度
        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        //Toast的Y坐标是屏幕高度的1/3，不会出现不适配的问题
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.TOP, 0, height / 3);
        mToast.setView(mToastRoot);
    }
    public  void showToast(int mToastType,String message) {
        //为控件设置属性
        mTvToastMessage.setText(message);
        switch (mToastType){
            case S_TOAST_NONE:
                mToastRoot.setBackgroundResource(R.drawable.sp_custom_toast_bg_none);
                mIvToastType.setVisibility(View.GONE);
                break;

            case S_TOAST_SUCCESS:
                mToastRoot.setBackgroundResource(R.drawable.sp_custom_toast_bg);
                mIvToastType.setVisibility(View.VISIBLE);
                mIvToastType.setImageResource(R.mipmap.icon_toast_success);
                break;

            case S_TOAST_FAIL:
                mToastRoot.setBackgroundResource(R.drawable.sp_custom_toast_bg);
                mIvToastType.setVisibility(View.VISIBLE);
                mIvToastType.setImageResource(R.mipmap.icon_toast_error);
                break;
        }
        mToast.show();
    }
}
